package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.YarnResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface YarnResourceDao extends JpaRepository<YarnResourceEntity, Integer>, JpaSpecificationExecutor<YarnResourceEntity> {
    YarnResourceEntity findFirstByQueueName(String queueName);

    YarnResourceEntity findFirstByOwner(Integer owner);

    @Query("select y from YarnResourceEntity y where owner=:owner and parentId=0 ")
    YarnResourceEntity findQueueByOwner(@Param("owner") Integer owner);

    List<YarnResourceEntity> findByParentId(Integer parentId);

    @Query("select y from YarnResourceEntity y where id !=:id and parentId=:parentId ")
    List<YarnResourceEntity> findByParentIdExceptSelf(@Param("parentId") Integer parentId, @Param("id") Integer id);

    YarnResourceEntity findFirstByQueueNameAndParentId(String queueName, Integer parentId);

    @Query("select y.queueName from YarnResourceEntity y where parentId=:parentId ")
    List<String> findQueueNameByParentId(@Param("parentId") Integer parentId);

    @Query("delete from YarnResourceEntity h where h.parentId=:parentId")
    @Modifying
    Integer deleteByParentId(@Param("parentId") Integer parentId);

    @Query("select h.id from YarnResourceEntity h where  parentId=:parentId ")
    List<Integer> findIdByParentId(@Param("parentId") Integer parentId);

    YarnResourceEntity findYarnResourceEntityById(Integer id);
}
